<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="素材分类" prop="inforId">
        <el-cascader
          clearable
          filterable
          :props="{expandTrigger: 'hover', value: 'id', label: 'inName', disabled: 'disabled', 'emitPath': false, 'checkStrictly': true}"
          v-model="queryParams.inforId"
          :options="aInforList"
          :show-all-levels="true"
        ></el-cascader>
      </el-form-item>
      <el-form-item label="查询详情" prop="infoDetails">
        <el-input
          v-model="queryParams.infoDetails"
          placeholder="请输入详情"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="权限" prop="agentLevel">
        <el-select v-model="queryParams.agentLevel" placeholder="请选择权限">
          <el-option
            v-for="dict in dict.type.agent_level"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="时间" prop="params">
        <el-date-picker
          v-model="dataPicker"
          type="daterange"
          value-format="yyyy-MM-dd"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:aInformation:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:aInformation:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:aInformation:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="aInformationList" @selection-change="handleSelectionChange" :key="updateTable">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="昵称" align="center" prop="adminName" />
      <el-table-column label="头像" align="center" prop="adminImg" >
        <template slot-scope="scope">
          <image-preview :src="scope.row.adminImg" :showImgNum="1" :width="50" :height="50"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="分享内容" align="center" prop="infoContent" />-->
      <el-table-column label="上传信息" align="center" prop="infoImage" width="250">
        <template slot-scope="scope">
          <image-preview :src="scope.row.infoImage" :showImgNum="3" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="详情" align="center" prop="infoDetails" >
        <template slot-scope="scope">
          <div v-html="scope.row.infoDetails" v-sub="30" v-copy></div>
        </template>
      </el-table-column>
      <el-table-column label="素材类型" align="center" prop="inName"  width="150"/>
      <el-table-column label="审核状态" align="center" prop="isState" >
        <template slot-scope="scope">
          <div v-if="scope.row.isState == 0">申请中</div>
          <div v-if="scope.row.isState == 1">成功</div>
          <div v-if="scope.row.isState == 2">驳回</div>
        </template>
      </el-table-column>
      <el-table-column label="点赞" align="center" prop="likeNum" width="50" />
      <el-table-column label="下载" align="center" prop="downloadNum" width="50" />
      <el-table-column label="收藏" align="center" prop="collectionNum"  width="50"/>
      <el-table-column label="分享" align="center" prop="shareNum" width="50" />
      <el-table-column label="权限" align="center" prop="agentLevel" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.agent_level" :value="scope.row.agentLevel"/>
        </template>
      </el-table-column>
      <el-table-column label="是否上线" align="center" prop="isShow" width="140">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.isShow" @change="handleSwitchChange(scope.row)"  active-text="上线" inactive-text="下线" />
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="numSoft" width="50"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="150">
      <template slot-scope="scope">
        <span>{{scope.row.createTime}}</span>
      </template>
    </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="getUserOperationList(scope.row)"
          >查看下载数</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:aInformation:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:aInformation:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改分享类型详细对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
<!--        <el-form-item label="标题">-->
<!--          <el-input v-model="form.infoContent" type="textarea" placeholder="分享内容" />-->
<!--        </el-form-item>-->
        <el-form-item label="素材类型" prop="inforId">
          <el-cascader
            clearable
            filterable
            :props="{expandTrigger: 'hover', value: 'id', label: 'inName', disabled: 'disabled', 'emitPath': false, 'checkStrictly': true}"
            v-model="form.inforId"
            :options="aInforList"
            :show-all-levels="true"
          ></el-cascader>
        </el-form-item>
        <!--        <el-form-item label="类型分类" prop="inforId2">
                  <el-select v-model="form.inforId2"  placeholder="请选择类型分类">
                    <el-option
                      v-for="item in childrenList"
                      :key="item.id"
                      :label="item.inName"
                      :value="parseInt(item.id)"
                    ></el-option>
                  </el-select>
                </el-form-item>-->
        <el-form-item label="权限" prop="agentLevel">
          <el-select v-model="form.agentLevel" placeholder="请选择权限">
            <el-option
              v-for="dict in dict.type.agent_level"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="审核状态" prop="infoDetails">
          <el-select v-model="form.isState" >
            <el-option value="0" label="申请中" />
            <el-option value="1" label="成功" />
            <el-option value="2" label="驳回" />
          </el-select>
        </el-form-item>
        <el-form-item label="是否上线" prop="isShow">
          <el-switch v-model="form.isShow" active-text="上线" inactive-text="下线" />
        </el-form-item>
        <el-form-item label="排序" prop="numSoft">
          <el-input-number v-model="form.numSoft" :min="0" :max="9999" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="上传信息" prop="infoImage">
          <image-upload v-model="form.infoImage" :fileSize="1000" :limit="10" :file-type="['png','jpg','jpeg','mp4']" />
        </el-form-item>
        <el-form-item label="详情" prop="infoDetails">
          <editor v-model="form.infoDetails" :min-height="192" :toolbar="[['emoji']]"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="downTitle" :visible.sync="downFlag" width="50%" append-to-body>
      <el-form :model="userParams" ref="userForm" size="small" :inline="true" v-show="showUserSearch" label-width="68px">
        <el-form-item label="昵称" prop="nickName">
            <el-input
              v-model="userParams.nickName"
              placeholder="请输入昵称"
              clearable
              @keyup.enter.native="handleUserQuery"
            />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input
            v-model="userParams.phone"
            placeholder="请输入手机号"
            clearable
            @keyup.enter.native="handleUserQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleUserQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetUserQuery">重置</el-button>
        </el-form-item>
      </el-form>

        <el-table v-loading="loading" :data="UserOperationList">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="昵称" align="center" prop="nickName" />
          <el-table-column label="头像" align="center" prop="headImg" >
            <template slot-scope="scope">
              <image-preview :src="scope.row.headImg" :width="50" :height="50"/>
            </template>
          </el-table-column>
          <el-table-column label="手机号" align="center" prop="phone" />
          <el-table-column label="创建时间" align="center" prop="createTime"/>
          <el-table-column label="下载数" align="center" prop="downNum" />
        </el-table>

        <pagination
          v-show="totals>0"
          :total="totals"
          :page.sync="userParams.pageNum"
          :limit.sync="userParams.pageSize"
          @pagination="getUserOperationList"
        />

    </el-dialog>
  </div>
</template>

<script>
import { listAInformation, getAInformation, delAInformation, addAInformation, updateAInformation } from "@/api/system/aInformation";
import { listAInfor } from "@/api/system/aInfor";
import { getUserOperList } from "@/api/system/aUser";
import {changeUserStatus} from "@/api/system/user";
import {handleTree} from "@/utils/ruoyi";

export default {
  name: "AInformation",
  dicts: ['agent_level'],
  data() {
    return {
      //inforId:"",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 分享类型详细表格数据
      aInformationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        infoContent: null,
        infoImage: null,
        infoDetails: null,
        likeNum: null,
        downloadNum: null,
        collectionNum: null,
        shareNum: null,
        agentLevel: null,
        inforId: null,
        isShow: null,
        numSoft: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        // infoContent: [
        //   { required: true, message: "分享内容不能为空", trigger: "blur" }
        // ],
        infoImage: [
          { required: true, message: "上传信息不能为空", trigger: "blur" }
        ],
        infoDetails: [
          { required: true, message: "详情不能为空", trigger: "blur" }
        ],
        isShow: [
          { required: true, message: "是否上线不能为空", trigger: "change" }
        ],
        numSoft: [
          { required: true, message: "排序不能为空", trigger: "blur" }
        ],
        inforId: [
          { required: true, message: "素材类型不能为空", trigger: "blur" }
        ],
        agentLevel: [
          { required: true, message: "权限不能为空", trigger: "change" }
        ],
      },
      //获取类型数据
      aInforList :[],
      downTitle :"查看下载信息",
      downFlag :false,
      UserOperationList:[],//获取下载收藏分享点赞的信息
      userParams:{
        pageNum: 1,
        pageSize: 10,
        nickName: null,
        phone: null,
        dataId: null,
        dataType:1,
      },
      totals: 0,
      // 显示搜索条件
      showUserSearch: true,
      userDataId:"",
      dataPicker:[],
      updateTable:true,
      childrenList:[],

    };
  },
  created() {
    this.getList();
    this.getInforList();
  },
  watch:{
    "scope.row.infoDetails":{
      handler(val){
        console.log(val)
      }
    }
  },
  methods: {
    childrenData(val){
      this.childrenList = [];
      let that = this;
      this.aInforList.forEach(function (el){
        if(el.id == val){
          that.childrenList = el.children;
          if(!that.childrenList[0]){
            that.form.inforId2 = null;
          }
        }
      })
    },
    /** 搜索按钮操作 */
    handleUserQuery() {
      this.userParams.pageNum = 1;
      this.queryList();
    },
    resetUserQuery(){
      this.userParams = {
          nickName: null,
          phone: null,
          dataId: null,
          dataType:1,
      },
      this.queryList();
    },
    getUserOperationList(row){
        this.userDataId = row.id
        this.queryList();
    },
    queryList(){
      this.userParams.dataId = this.userDataId;
      getUserOperList(this.userParams).then(res =>{
        this.UserOperationList = res.rows;
        this.totals = res.total;
        this.downFlag = true;
      })
    },
    handleSwitchChange(row){
      updateAInformation(row).then(res=>{
        if(res.code == 200){
          this.$modal.msgSuccess("修改成功");
          this.getList();
        }else{
          this.$modal.msgError(res.msg);
        }
      })
    },
    /** 查询分享类型详细列表 */
    getList() {
      this.loading = true;
      if (this.dataPicker && this.dataPicker.length  && this.dataPicker[0] && this.dataPicker[1]){
        this.queryParams.params = {}
        this.queryParams.params.startTime = this.dataPicker[0] + " 00:00:00";
        this.queryParams.params.endTime = this.dataPicker[1] + " 23:59:59";
      }
      listAInformation(this.queryParams).then(response => {
        this.aInformationList = response.rows;
        this.total = response.total;
        this.loading = false;
        this.updateTable = !this.updateTable;

      });
    },
    /** 获取父类的所有数据 */
    getInforList(){
      listAInfor().then(response => {
        let treeData = handleTree(response.data);
        this.aInforList = this.handleDisable(treeData);
      });
    },
    handleDisable(data) {
      data.forEach(item =>{
        if(item.ancestors.split(",").length < 2){
          item.disabled = true;
        }
      });
      return data;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        infoContent: null,
        infoImage: null,
        infoDetails: null,
        likeNum: null,
        downloadNum: null,
        collectionNum: null,
        shareNum: null,
        agentLevel: null,
        inforId: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null,
        delFlag: null,
        isShow: null,
        numSoft: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.params = {}
      this.dataPicker = []
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.getInforList();
      this.reset();
      this.open = true;
      this.title = "添加分享类型详细";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.getInforList();
      this.reset();
      const id = row.id || this.ids
      getAInformation(id).then(response => {
        this.form = response.data;
        //this.inforId = this.form.inforId;
        //this.form.inforId = this.form.inName;
        this.open = true;
        this.title = "修改分享类型详细";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            /*if(this.form.inName == this.form.inforId){
              this.form.inforId = this.inforId;
            }*/
            updateAInformation(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
              this.getInforList();
            });
          } else {
            addAInformation(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
              this.getInforList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除分享类型详细编号为"' + ids + '"的数据项？').then(function() {
        return delAInformation(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/aInformation/export', {
        ...this.queryParams
      }, `aInformation_${new Date().getTime()}.xlsx`)
    },
  }
};
</script>
